package com.hebeu.device.service.impl;

import com.hebeu.device.dao.DPartMapper;
import com.hebeu.device.dao.DPartFluidMapper;
import com.hebeu.device.pojo.DPart;
import com.hebeu.device.pojo.DPartExample;
import com.hebeu.device.pojo.DPartFluid;
import com.hebeu.device.service.PartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PartServiceImpl implements PartService {
    private DPartMapper dPartMapper;
    private DPartFluidMapper dPartFluidMapper;

    @Autowired
    public PartServiceImpl(DPartMapper dPartMapper, DPartFluidMapper dPartFluidMapper) {
        this.dPartMapper = dPartMapper;
        this.dPartFluidMapper = dPartFluidMapper;
    }

    @Override
    public List<DPart> selectAllDparts() {
        List<DPart> dParts = dPartMapper.selectByExample(new DPartExample());
        return dParts;
    }

    @Override
    public void addPart(DPart dPart) {
        if (dPart.getNumber().equals(dPart.getAlertNumber())) {
            dPart.setPartStatus(2);
        } else if (dPart.getNumber() > dPart.getAlertNumber()) {
            dPart.setPartStatus(1);
        } else if (dPart.getNumber() < dPart.getAlertNumber()) {
            dPart.setPartStatus(3);
        }else if (dPart.getNumber() ==0) {
            dPart.setPartStatus(4);
        }
        dPartMapper.insertSelective(dPart);
    }

    @Override
    public DPartFluid addPartFluid(DPart dPart) {
        DPartFluid dPartFluid = new DPartFluid();
        dPartFluid.setMainId(0);
        dPartFluid.setCreatetime(new Date());
        dPartFluid.setNumber(dPart.getNumber());
        dPartFluid.setPartName(dPart.getPartName());
        dPartFluid.setPartType(dPart.getPartType());
        dPartFluidMapper.insertSelective(dPartFluid);
        return dPartFluid;
    }
    @Override
    public DPart getPartById(Integer partId) {
        return dPartMapper.selectByPrimaryKey(partId);
    }
    @Override
    public void updatePart(DPart dPart){
        if (dPart.getNumber()==dPart.getAlertNumber()){
            dPart.setPartStatus(2);
        }else if (dPart.getNumber()>dPart.getAlertNumber()){
            dPart.setPartStatus(1);
        }else if (dPart.getNumber()<dPart.getAlertNumber()){
            dPart.setPartStatus(3);
        }else if (dPart.getNumber()==0){
            dPart.setPartStatus(0);
        }
        dPartMapper.updateByPrimaryKeySelective(dPart);
    }
}
